package com.dhgate.spu.analysis.init;

import java.io.File;

import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhgate.spu.analysis.consts.SysConsts;

/**
 * @author: wangguodong
 * @create: 2014-2-15 上午10:15:19
 * @description:
 */
public class InitParameter {
	protected static final Logger logger = LoggerFactory.getLogger(InitParameter.class);

	public static void init() throws Exception {
		try {
			logger.info("InitParameter begin");
			// 获得XML文档对象
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(SysConsts.CONST_PARAMXML_PATH));
			SysConsts.DAO_NAME = document.getRootElement().element("DAO").getTextTrim();
			Element solrElement = document.getRootElement().element("SOLR");
			SysConsts.SOLR_URL = solrElement.elementTextTrim("URL");
			SysConsts.SOLR_BATCH_ADD_SIZE = Integer.parseInt(solrElement.elementTextTrim("BATCHADDSIZE"));
			SysConsts.SOLR_TIME_OUT = Integer.parseInt(solrElement.elementTextTrim("TIMEOUT"));

			SysConsts.SOLR_SERVER = new HttpSolrServer(SysConsts.SOLR_URL);
			SysConsts.SOLR_SERVER.setSoTimeout(SysConsts.SOLR_TIME_OUT); // socket read timeout
			SysConsts.SOLR_SERVER.setConnectionTimeout(SysConsts.SOLR_TIME_OUT);
			SysConsts.SOLR_SERVER.setDefaultMaxConnectionsPerHost(100);
			SysConsts.SOLR_SERVER.setMaxTotalConnections(100);
			SysConsts.SOLR_SERVER.setFollowRedirects(false); // defaults to
			SysConsts.SOLR_SERVER.setAllowCompression(true);
			SysConsts.SOLR_SERVER.setMaxRetries(2);

			logger.info("InitParameter end");
		} catch (Exception e) {
			logger.error("error:" + e);
			throw new Exception("error:" + e);
		}
	}
}
